package com.macro.mall.portal.service.impl;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.api.ResultCode;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.UmsMemberLogMapper;
import com.macro.mall.model.UmsMemberLog;
import com.macro.mall.portal.service.UmsMemberLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
/**
 * UmsMemberLog的服务接口的实现类
 * 
 * @author gaodx
 *
 */
@Slf4j
@Service
public class UmsMemberLogServiceImpl implements UmsMemberLogService {

    private boolean isDebugEnabled = true;

	@Autowired
	private UmsMemberLogMapper umsMemberLogMapper;
	// TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化

	@Override
	public List<UmsMemberLog> find(UmsMemberLog value) {
		//TODO这里可以做通过Assist做添加查询
        Assist assist = new Assist();
        List<UmsMemberLog> result = null;
        try {
            if(StrUtil.isNotEmpty(value.getPhone())) {
                assist.andEq("phone", value.getPhone());
                assist.setOrder(Assist.order("create_Time",false));
                assist.setRowSize(1);
                assist.setStartRow(1);
            }
            result = umsMemberLogMapper.selectUmsMemberLog(assist);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
        if (isDebugEnabled) {
            log.debug("执行获取UmsMemberLog数据集-->结果:", result);
        }
		return result;
	}
	@Override
	public UmsMemberLog findOne(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过UmsMemberLog的id获得UmsMemberLog对象-->失败:id不能为空");
			}
            Asserts.fail("执行通过UmsMemberLog的id获得UmsMemberLog对象-->失败:id不能为空");
		}
		UmsMemberLog result = null;
        try {
            result = umsMemberLogMapper.selectUmsMemberLogById(id, null);
        } catch (Exception e) {
        log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行通过UmsMemberLog的id获得UmsMemberLog对象-->结果:", result);
		}
		return result;
	}

	@Override
	public CommonResult saveNotNull(UmsMemberLog value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行将UmsMemberLog中属性值不为null的数据保存到数据库-->失败:对象不能为空");
			}
                Asserts.fail("失败:对象不能为空");
		}
		int result = 0;
        try {
            result = umsMemberLogMapper.insertNotNullUmsMemberLog(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行将UmsMemberLog中属性值不为null的数据保存到数据库-->结果:", result);
		}
            return CommonResult.success(null);
	}
	@Override
	public CommonResult updateNotNullById(UmsMemberLog value) {
		if (value == null) {
			if (isDebugEnabled) {
				log.debug("执行通过UmsMemberLog的id更新UmsMemberLog中属性不为null的数据-->失败:对象为null");
			}
                Asserts.fail("执行通过UmsMemberLog的id更新UmsMemberLog中属性不为null的数据-->失败:对象为null");
		}
		int result = 0;
        try {
            result = umsMemberLogMapper.updateNotNullUmsMemberLogById(value);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行通过UmsMemberLog的id更新UmsMemberLog中属性不为null的数据-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
	public CommonResult deleteById(Long id) {
		if (id == null) {
			if (isDebugEnabled) {
				log.debug("执行通过UmsMemberLog的id删除UmsMemberLog-->失败:id不能为空");
			}
                Asserts.fail("执行通过UmsMemberLog的id删除UmsMemberLog-->失败:id不能为空");
		}
		int result = 0;
        try {
            result = umsMemberLogMapper.deleteUmsMemberLogById(id);
        } catch (Exception e) {
            log.error("", e);
            Asserts.fail(ResultCode.FAILED);
        }
		if (isDebugEnabled) {
			log.debug("执行通过UmsMemberLog的id删除UmsMemberLog-->结果:", result);
		}
        return CommonResult.success(null);
	}

	@Override
	public UmsMemberLog findOneNewestComplete(Long userId) {
		if (userId == null) {
			if (isDebugEnabled) {
				log.debug("执行通过UmsMember的id和是否是补全页更新的获得UmsMemberLog对象-->失败:id不能为空");
			}
			Asserts.fail("执行通过UmsMember的id和是否是补全页更新的获得UmsMemberLog对象-->失败:id不能为空");
		}
		UmsMemberLog result = null;
		try {
			result = umsMemberLogMapper.selectUmsMemberLogCompleteById(userId);
		} catch (Exception e) {
			log.error("", e);
			Asserts.fail(ResultCode.FAILED);
		}
		if (isDebugEnabled) {
			log.debug("执行通过UmsMember的id和是否是补全页更新的获得UmsMemberLog对象-->结果:", result);
		}
		return result;
	}


}